<template>
  <div class="exam-detail">
    <el-form :inline="true" :model="formData" :rules="rules" ref="formData">
      <el-form-item label="考试题目" prop="examTopic">
        <el-input
          v-model="formData.examTopic"
          placeholder="考试题目"
          autocomplete="off"
          :clearable="true"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
        <el-button type="info" @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <div class="admin-table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="tableLoading"
      >
        <el-table-column
          type="index"
          width="49"
          :index="indexConfig"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column
          label="考试题目"
          prop="examTopic"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="正确答案"
          prop="topicRightOption"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="用户答案"
          prop="topicUserOption"
          width="330"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="题目分值"
          prop="topicScore"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 1 ? 'success' : 'danger'"
              disable-transitions
            >
              {{ scope.row.status === 1 ? "正确" : "错误" }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        v-show="tableData.length > 0"
        background
        layout="prev, pager, next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        :pager-count="7"
        style="text-align: right; margin-top: 24px"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getExamDetail } from "@/api/admin/exam";
export default {
  name: "ExamDetailView",
  data() {
    return {
      formData: {
        examId: "",
        examTopic: "",
      },
      rules: {
        examTopic: [{ validator: this.customValidator, trigger: "blur" }],
      },
      page: 1,
      pageSize: 8,
      total: 0,
      currentPage: 1,
      tableData: [],
      tableLoading: false,
    };
  },
  props: ["id"],
  created() {
    this.formData.examId = this.id;
    this.getDataList();
  },
  methods: {
    // 自定义校验规则
    customValidator(rule, value, callback) {
      const { examTopic } = this.formData;
      if (!examTopic) {
        callback(new Error("请填写信息"));
      } else {
        callback();
      }
    },
    // 分页查询
    query() {
      this.$refs.formData.validate(async (v) => {
        if (v) {
          this.page = 1;
          this.getDataList();
        }
      });
    },
    // 清空新增表单
    clear() {
      this.page = 1;
      this.currentPage = 1;
      this.$refs.formData.resetFields();
      this.getDataList();
    },
    // 配置表格索引
    indexConfig(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getDataList();
    },
    // 获取数据
    async getDataList() {
      try {
        this.tableLoading = true;
        const res = await getExamDetail({
          ...this.formData,
          page: this.page,
          pageSize: this.pageSize,
        });
        this.tableData = res.data.records;
        this.total = res.data.total;
      } finally {
        this.tableLoading = false;
      }
    },
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      let hour = this.padZero(timeArray[3]);
      let minute = this.padZero(timeArray[4]);
      let second = this.padZero(timeArray[5]) || "00";
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
  },
};
</script>

<style lang="less" scoped>
.exam-detail {
  width: 100%;
  height: 100%;
  background-color: #fff;
  .insertForm {
    width: 440px;
  }
}

// 解决下拉框显示不完整问题
/deep/.el-select-dropdown__wrap.el-scrollbar__wrap {
  margin-bottom: 0 !important;
}

/*
解决单选框样式问题
  Blocked aria-hidden on an element because its descendant retained focus. 
  The focus must not be hidden from assistive technology users. 
  Avoid using aria-hidden on a focused element or its ancestor. 
  Consider using the inert attribute instead, which will also prevent focus. 
  For more details, see the aria-hidden section of the WAI-ARIA specification at Element with focus: input Ancestor with aria-hidden:
*/
/deep/ input[aria-hidden="true"] {
  display: none !important;
}
/deep/.el-radio:focus:not(.is-focus):not(:active):not(.is-disabled)
  .el-radio__inner {
  box-shadow: none !important;
}
</style>